package com.first.community.controller;

import com.first.community.dto.CommentDTO;
import com.first.community.dto.QuestionDTO;
import com.first.community.enums.commentTypeEnums;
import com.first.community.service.CommentService;
import com.first.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;


    @GetMapping("/question/{number}")
    public String question(@PathVariable() Long number,
                           Model model
    ) {
        QuestionDTO questionDTO = questionService.getById(number);

//        查询与该问题相关的问题，用标签匹配
        List<QuestionDTO> relatedQuestions = questionService.selectByTag(questionDTO);
        model.addAttribute("relatedQuestions",relatedQuestions);


//        追加回复列表
        List<CommentDTO> comments=commentService.ListById(number, commentTypeEnums.QUESTION);
//        添加评论数
        questionService.addViewCount(questionDTO.getId());
        model.addAttribute("question", questionDTO);
        model.addAttribute("comments", comments);
        return "question";
    }
}
